<?php

declare (strict_types=1);

namespace App\Model;

use Hyperf\Database\Model\Relations\HasMany;
use Hyperf\Database\Model\Relations\HasOne;
use Hyperf\Database\Query\Builder;

/**
 * @property int $id
 * @property int $shop_id 商家ID
 * @property int $user_id 申请人
 * @property int $vip_order_id 会员订单ID
 * @property int $apply_time 申请时间
 * @property int $apply_type 申请类型： 1个人 2个体工商户 3企业
 * @property int $status 状态：1待审核 2审核通过 3被驳回
 * @property int $check_user 审核人
 * @property int $check_time 审核时间
 * @property string $refuse_remark 驳回原因
 */
class FlyShopApply extends Model
{
    /**
     * 关闭自动维护时间
     * @var bool
     */
    public $timestamps = false;

    /**
     * 数据库时间类型：时间戳
     * @var string
     */
//    protected $dateFormat = 'U';

    /**
     * 模型关联表
     * @var string
     */
    protected $table = 'fly_shop_applys';

    /**
     * 可以批量赋值的白名单 || $guarded[]不可以批量赋值的白名单
     * @var array
     */
    protected $fillable = [];

    /**
     * 被模型转换的类型
     * @var array
     */
    protected $casts = [
        'id' => 'integer',
        'shop_id' => 'integer',
        'user_id' => 'string',
        'vip_order_id' => 'integer',
        'apply_time' => 'string',
        'apply_type' => 'integer',
        'status' => 'integer',
        'check_user' => 'integer',
        'figure_urls' => 'array',
        'check_time' => 'integer'
    ];

    /**
     * 获取店铺关联
     * @return HasOne
     */
    public function with_shop()
    {
        return $this->hasOne(FlyShop::class, 'shop_id', 'id')
            ->with(['with_certs', 'with_jsd', 'with_hy']);
    }


    /**
     * 获取商家附件信息
     * @return HasMany|Builder
     */
    public function with_certs()
    {
        return $this->hasMany(FlyShopCertificate::class, 'shop_id', 'shop_id')
            ->leftJoin('fly_shop_certificate_att as att', 'fly_shop_certificate.id', '=', 'att.shop_id')
            ->leftJoin('fileinfo as f', 'att.fileinfo_id', '=', 'f.fileinfo_id')
            ->select(['fly_shop_certificate.*', 'att.id as att_id', 'att.type', 'f.fileinfo_id', 'f.url']);
    }

    /**
     * 关联申请详情
     * @return HasOne
     */
    public function apply_info()
    {
        return $this->hasOne(FlyShopApplysInfo::class, 'apply_id', 'id');
    }

}